<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="一年春又来">
<meta property="og:url" content="http://littlefxc.github.io/page/12/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="一年春又来">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/page/12/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>
<title>一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/Rabbitmq%E9%AB%98%E7%BA%A7%E7%89%B9%E6%80%A7-%E7%94%9F%E4%BA%A7%E7%AB%AF%E5%8F%AF%E9%9D%A0%E6%80%A7%E6%8A%95%E9%80%92%E4%B8%8E%E6%B6%88%E8%B4%B9%E7%AB%AF%E5%B9%82%E7%AD%89%E6%80%A7-19/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/Rabbitmq%E9%AB%98%E7%BA%A7%E7%89%B9%E6%80%A7-%E7%94%9F%E4%BA%A7%E7%AB%AF%E5%8F%AF%E9%9D%A0%E6%80%A7%E6%8A%95%E9%80%92%E4%B8%8E%E6%B6%88%E8%B4%B9%E7%AB%AF%E5%B9%82%E7%AD%89%E6%80%A7-19/" class="post-title-link" itemprop="url">Rabbitmq高级特性-生产端可靠性投递与消费端幂等性(19)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 22:52:40" itemprop="dateCreated datePublished" datetime="2021-01-25T22:52:40+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_1.png" alt="rabbitmq_19_1"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_2.png" alt="rabbitmq_19_2"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_4.png" alt="rabbitmq_19_4"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_5.png" alt="rabbitmq_19_5"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_6.png" alt="rabbitmq_19_6"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_7.png" alt="rabbitmq_19_7"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_8.png" alt="rabbitmq_19_8"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_9.png" alt="rabbitmq_19_9"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_19_12.png" alt="rabbitmq_19_12"></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/RabbitMQ%E9%9B%86%E7%BE%A4%E6%9E%B6%E6%9E%84%E6%A8%A1%E5%9E%8B%E4%B8%8E%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90-5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/RabbitMQ%E9%9B%86%E7%BE%A4%E6%9E%B6%E6%9E%84%E6%A8%A1%E5%9E%8B%E4%B8%8E%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90-5/" class="post-title-link" itemprop="url">RabbitMQ集群架构模型与原理解析(5)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 22:41:51" itemprop="dateCreated datePublished" datetime="2021-01-25T22:41:51+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_4_5.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_4_5.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_2.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_2.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_3.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_3.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_4.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_4.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_11.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_11.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_10.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_10.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_5.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_5.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_6.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_6.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_7.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_7.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_8.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_8.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_9.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_9.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_12.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_12.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_13.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_13.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_14.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_14.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_15.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_15.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_16.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_16.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_17.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_17.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_18.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_18.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_18.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_5_18.png"></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/Rabbitmq%E6%A0%B8%E5%BF%83API-%E5%85%B6%E4%BB%96%E5%85%B3%E9%94%AE%E6%A6%82%E5%BF%B5%E8%AE%B2%E8%A7%A3-18/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/Rabbitmq%E6%A0%B8%E5%BF%83API-%E5%85%B6%E4%BB%96%E5%85%B3%E9%94%AE%E6%A6%82%E5%BF%B5%E8%AE%B2%E8%A7%A3-18/" class="post-title-link" itemprop="url">Rabbitmq核心API-其他关键概念讲解(18)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 22:39:41" itemprop="dateCreated datePublished" datetime="2021-01-25T22:39:41+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_1.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_1.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_2.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_2.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_3.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_3.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_5.png" alt="rabbitmq_18_5"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_18_6.png" alt="rabbitmq_18_6"></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/Rabbitmq%E6%A0%B8%E5%BF%83API-Exchange%E4%B9%8BFanout-17/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/Rabbitmq%E6%A0%B8%E5%BF%83API-Exchange%E4%B9%8BFanout-17/" class="post-title-link" itemprop="url">Rabbitmq核心API-Exchange之Fanout(17)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 22:18:31" itemprop="dateCreated datePublished" datetime="2021-01-25T22:18:31+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_17_1.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_17_1.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_17_2.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_17_2.png"></p>
<h2 id="Sender4FanoutExchange"><a href="#Sender4FanoutExchange" class="headerlink" title="Sender4FanoutExchange"></a>Sender4FanoutExchange</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Sender4FanoutExchange</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//1 创建ConnectionFactory</span></span><br><span class="line">		ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory();</span><br><span class="line">		connectionFactory.setHost(<span class="string">&quot;192.168.11.71&quot;</span>);</span><br><span class="line">		connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//2 创建Connection</span></span><br><span class="line">		Connection connection = connectionFactory.newConnection();</span><br><span class="line">		<span class="comment">//3 创建Channel</span></span><br><span class="line">		Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_fanout_exchange&quot;</span>;</span><br><span class="line">		<span class="comment">//5 发送</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i ++) &#123;</span><br><span class="line">			String msg = <span class="string">&quot;Hello World RabbitMQ 4 FANOUT Exchange Message ...&quot;</span>;</span><br><span class="line">			channel.basicPublish(exchangeName, <span class="string">&quot;11&quot;</span> , <span class="keyword">null</span> , msg.getBytes()); 			</span><br><span class="line">		&#125;</span><br><span class="line">		channel.close();  </span><br><span class="line">        connection.close();  </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Receiver4FanoutExchange1"><a href="#Receiver4FanoutExchange1" class="headerlink" title="Receiver4FanoutExchange1"></a>Receiver4FanoutExchange1</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Receiver4FanoutExchange1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">        ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory() ;  </span><br><span class="line">        </span><br><span class="line">        connectionFactory.setHost(<span class="string">&quot;192.168.11.71&quot;</span>);</span><br><span class="line">        connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">        connectionFactory.setAutomaticRecoveryEnabled(<span class="keyword">true</span>);</span><br><span class="line">        connectionFactory.setNetworkRecoveryInterval(<span class="number">3000</span>);</span><br><span class="line">        Connection connection = connectionFactory.newConnection();</span><br><span class="line">        </span><br><span class="line">        Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_fanout_exchange&quot;</span>;</span><br><span class="line">		String exchangeType = <span class="string">&quot;fanout&quot;</span>;</span><br><span class="line">		String queueName = <span class="string">&quot;test_fanout_queue&quot;</span>;</span><br><span class="line">		String routingKey = <span class="string">&quot;&quot;</span>;	<span class="comment">//	不设置路由键</span></span><br><span class="line">		channel.exchangeDeclare(exchangeName, exchangeType, <span class="keyword">true</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueDeclare(queueName, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueBind(queueName, exchangeName, routingKey);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//durable 是否持久化消息</span></span><br><span class="line">        QueueingConsumer consumer = <span class="keyword">new</span> QueueingConsumer(channel);</span><br><span class="line">        <span class="comment">//参数：队列名称、是否自动ACK、Consumer</span></span><br><span class="line">        channel.basicConsume(queueName, <span class="keyword">true</span>, consumer); </span><br><span class="line">        System.err.println(<span class="string">&quot;--------------- consumer 1 --------------&quot;</span>);</span><br><span class="line">        <span class="comment">//循环获取消息  </span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;  </span><br><span class="line">            <span class="comment">//获取消息，如果没有消息，这一步将会一直阻塞  </span></span><br><span class="line">            Delivery delivery = consumer.nextDelivery();  </span><br><span class="line">            String msg = <span class="keyword">new</span> String(delivery.getBody());    </span><br><span class="line">            System.out.println(<span class="string">&quot;收到消息：&quot;</span> + msg);  </span><br><span class="line">        &#125; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Receiver4FanoutExchange2"><a href="#Receiver4FanoutExchange2" class="headerlink" title="Receiver4FanoutExchange2"></a>Receiver4FanoutExchange2</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Receiver4FanoutExchange2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">        ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory() ;  </span><br><span class="line">        </span><br><span class="line">        connectionFactory.setHost(<span class="string">&quot;192.168.11.71&quot;</span>);</span><br><span class="line">        connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">        connectionFactory.setAutomaticRecoveryEnabled(<span class="keyword">true</span>);</span><br><span class="line">        connectionFactory.setNetworkRecoveryInterval(<span class="number">3000</span>);</span><br><span class="line">        Connection connection = connectionFactory.newConnection();</span><br><span class="line">        </span><br><span class="line">        Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_fanout_exchange&quot;</span>;</span><br><span class="line">		String exchangeType = <span class="string">&quot;fanout&quot;</span>;</span><br><span class="line">		String queueName = <span class="string">&quot;test_fanout_queue&quot;</span>;</span><br><span class="line">		String routingKey = <span class="string">&quot;&quot;</span>;	<span class="comment">//	不设置路由键</span></span><br><span class="line">		channel.exchangeDeclare(exchangeName, exchangeType, <span class="keyword">true</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueDeclare(queueName, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueBind(queueName, exchangeName, routingKey);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//durable 是否持久化消息</span></span><br><span class="line">        QueueingConsumer consumer = <span class="keyword">new</span> QueueingConsumer(channel);</span><br><span class="line">        <span class="comment">//参数：队列名称、是否自动ACK、Consumer</span></span><br><span class="line">        channel.basicConsume(queueName, <span class="keyword">true</span>, consumer);  </span><br><span class="line">        System.err.println(<span class="string">&quot;--------------- consumer 2 --------------&quot;</span>);</span><br><span class="line">        <span class="comment">//循环获取消息  </span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;  </span><br><span class="line">            <span class="comment">//获取消息，如果没有消息，这一步将会一直阻塞  </span></span><br><span class="line">            Delivery delivery = consumer.nextDelivery();  </span><br><span class="line">            String msg = <span class="keyword">new</span> String(delivery.getBody());    </span><br><span class="line">            System.out.println(<span class="string">&quot;收到消息：&quot;</span> + msg);  </span><br><span class="line">        &#125; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/RabbitMQ%E6%A0%B8%E5%BF%83API-Exchange%E4%B9%8BTopic-16/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/RabbitMQ%E6%A0%B8%E5%BF%83API-Exchange%E4%B9%8BTopic-16/" class="post-title-link" itemprop="url">RabbitMQ核心API Exchange之Topic(16)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 21:51:45" itemprop="dateCreated datePublished" datetime="2021-01-25T21:51:45+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_16_1.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_16_1.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_16_2.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_16_2.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_16_3.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_16_3.png"></p>
<h2 id="Sender4TopicExchange"><a href="#Sender4TopicExchange" class="headerlink" title="Sender4TopicExchange"></a>Sender4TopicExchange</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Sender4TopicExchange</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//1 创建ConnectionFactory</span></span><br><span class="line">		ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory();</span><br><span class="line">		connectionFactory.setHost(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">		connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//2 创建Connection</span></span><br><span class="line">		Connection connection = connectionFactory.newConnection();</span><br><span class="line">		<span class="comment">//3 创建Channel</span></span><br><span class="line">		Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_topic_exchange&quot;</span>;</span><br><span class="line">		String routingKey1 = <span class="string">&quot;user.save&quot;</span>;</span><br><span class="line">		String routingKey2 = <span class="string">&quot;user.update&quot;</span>;</span><br><span class="line">		String routingKey3 = <span class="string">&quot;user.delete.abc&quot;</span>;</span><br><span class="line">		<span class="comment">//5 发送</span></span><br><span class="line">		</span><br><span class="line">		String msg = <span class="string">&quot;Hello World RabbitMQ 4 Topic Exchange Message ...&quot;</span>;</span><br><span class="line">		channel.basicPublish(exchangeName, routingKey1 , <span class="keyword">null</span> , msg.getBytes()); </span><br><span class="line">		channel.basicPublish(exchangeName, routingKey2 , <span class="keyword">null</span> , msg.getBytes()); 	</span><br><span class="line">		channel.basicPublish(exchangeName, routingKey3 , <span class="keyword">null</span> , msg.getBytes()); </span><br><span class="line">		channel.close();  </span><br><span class="line">        connection.close();  </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Receiver4TopicExchange1"><a href="#Receiver4TopicExchange1" class="headerlink" title="Receiver4TopicExchange1"></a>Receiver4TopicExchange1</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Receiver4TopicExchange1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory() ;  </span><br><span class="line">        </span><br><span class="line">        connectionFactory.setHost(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">        connectionFactory.setAutomaticRecoveryEnabled(<span class="keyword">true</span>);</span><br><span class="line">        connectionFactory.setNetworkRecoveryInterval(<span class="number">3000</span>);</span><br><span class="line">        Connection connection = connectionFactory.newConnection();</span><br><span class="line">        </span><br><span class="line">        Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_topic_exchange&quot;</span>;</span><br><span class="line">		String exchangeType = <span class="string">&quot;topic&quot;</span>;</span><br><span class="line">		String queueName = <span class="string">&quot;test_topic_queue&quot;</span>;</span><br><span class="line">		<span class="comment">//String routingKey = &quot;user.*&quot;;</span></span><br><span class="line">		String routingKey = <span class="string">&quot;user.#&quot;</span>;</span><br><span class="line">		channel.exchangeDeclare(exchangeName, exchangeType, <span class="keyword">true</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueDeclare(queueName, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueBind(queueName, exchangeName, routingKey);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//durable 是否持久化消息</span></span><br><span class="line">        QueueingConsumer consumer = <span class="keyword">new</span> QueueingConsumer(channel);</span><br><span class="line">        <span class="comment">//	参数：队列名称、是否自动ACK、Consumer</span></span><br><span class="line">        channel.basicConsume(queueName, <span class="keyword">true</span>, consumer);</span><br><span class="line">        System.err.println(<span class="string">&quot;consumer1 start.. &quot;</span>);</span><br><span class="line">        <span class="comment">//	循环获取消息  </span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;  </span><br><span class="line">            <span class="comment">//	获取消息，如果没有消息，这一步将会一直阻塞  </span></span><br><span class="line">            Delivery delivery = consumer.nextDelivery();  </span><br><span class="line">            String msg = <span class="keyword">new</span> String(delivery.getBody());    </span><br><span class="line">            System.out.println(<span class="string">&quot;收到消息：&quot;</span> + msg + <span class="string">&quot;, RoutingKey: &quot;</span> + delivery.getEnvelope().getRoutingKey());  </span><br><span class="line">        &#125; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Receiver4TopicExchange2"><a href="#Receiver4TopicExchange2" class="headerlink" title="Receiver4TopicExchange2"></a>Receiver4TopicExchange2</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Receiver4TopicExchange2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory() ;  </span><br><span class="line">        </span><br><span class="line">        connectionFactory.setHost(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">        connectionFactory.setAutomaticRecoveryEnabled(<span class="keyword">true</span>);</span><br><span class="line">        connectionFactory.setNetworkRecoveryInterval(<span class="number">3000</span>);</span><br><span class="line">        Connection connection = connectionFactory.newConnection();</span><br><span class="line">        </span><br><span class="line">        Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_topic_exchange&quot;</span>;</span><br><span class="line">		String exchangeType = <span class="string">&quot;topic&quot;</span>;</span><br><span class="line">		String queueName = <span class="string">&quot;test_topic_queue&quot;</span>;</span><br><span class="line">		String routingKey = <span class="string">&quot;user.*&quot;</span>;</span><br><span class="line"><span class="comment">//		String routingKey = &quot;user.#&quot;;</span></span><br><span class="line">		channel.exchangeDeclare(exchangeName, exchangeType, <span class="keyword">true</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueDeclare(queueName, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueBind(queueName, exchangeName, routingKey);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//durable 是否持久化消息</span></span><br><span class="line">        QueueingConsumer consumer = <span class="keyword">new</span> QueueingConsumer(channel);</span><br><span class="line">        <span class="comment">//参数：队列名称、是否自动ACK、Consumer</span></span><br><span class="line">        channel.basicConsume(queueName, <span class="keyword">true</span>, consumer); </span><br><span class="line">        System.err.println(<span class="string">&quot;consumer2 start .. &quot;</span>);</span><br><span class="line">        <span class="comment">//	循环获取消息  </span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;  </span><br><span class="line">            <span class="comment">//获取消息，如果没有消息，这一步将会一直阻塞  </span></span><br><span class="line">            Delivery delivery = consumer.nextDelivery();  </span><br><span class="line">            String msg = <span class="keyword">new</span> String(delivery.getBody());    </span><br><span class="line">            System.out.println(<span class="string">&quot;收到消息：&quot;</span> + msg + <span class="string">&quot;, RoutingKey: &quot;</span> + delivery.getEnvelope().getRoutingKey());  </span><br><span class="line">        &#125; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/RabbitMQ%E6%A0%B8%E5%BF%83API-Exchange%E4%B9%8BDirect-15/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/RabbitMQ%E6%A0%B8%E5%BF%83API-Exchange%E4%B9%8BDirect-15/" class="post-title-link" itemprop="url">RabbitMQ Exchange之Direct(15)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 21:49:11" itemprop="dateCreated datePublished" datetime="2021-01-25T21:49:11+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_3.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_3.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_5.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_5.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_6.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_6.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_7.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_7.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_8.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_8.png"></p>
<h2 id="生产者"><a href="#生产者" class="headerlink" title="生产者"></a>生产者</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Sender4DirectExchange</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//1 创建ConnectionFactory</span></span><br><span class="line">		ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory();</span><br><span class="line">		connectionFactory.setHost(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">		connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//2 创建Connection</span></span><br><span class="line">		Connection connection = connectionFactory.newConnection();</span><br><span class="line">		<span class="comment">//3 创建Channel</span></span><br><span class="line">		Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_direct_exchange&quot;</span>;</span><br><span class="line">		String routingKey = <span class="string">&quot;test_direct_routingKey&quot;</span>;</span><br><span class="line">		<span class="comment">//5 发送</span></span><br><span class="line">		</span><br><span class="line">		String msg = <span class="string">&quot;Hello World RabbitMQ 4  Direct Exchange Message ... &quot;</span>;</span><br><span class="line">		channel.basicPublish(exchangeName, routingKey , <span class="keyword">null</span> , msg.getBytes()); 		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="消费者"><a href="#消费者" class="headerlink" title="消费者"></a>消费者</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Receiver4DirectExchange</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory() ;  </span><br><span class="line">        </span><br><span class="line">        connectionFactory.setHost(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">        connectionFactory.setAutomaticRecoveryEnabled(<span class="keyword">true</span>);</span><br><span class="line">        connectionFactory.setNetworkRecoveryInterval(<span class="number">3000</span>);</span><br><span class="line">        Connection connection = connectionFactory.newConnection();</span><br><span class="line">        </span><br><span class="line">        Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//4 声明</span></span><br><span class="line">		String exchangeName = <span class="string">&quot;test_direct_exchange&quot;</span>;</span><br><span class="line">		String exchangeType = <span class="string">&quot;direct&quot;</span>;</span><br><span class="line">		String queueName = <span class="string">&quot;test_direct_queue&quot;</span>;</span><br><span class="line">		String routingKey = <span class="string">&quot;test_direct_routingKey&quot;</span>;</span><br><span class="line">		channel.exchangeDeclare(exchangeName, exchangeType, <span class="keyword">true</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueDeclare(queueName, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		channel.queueBind(queueName, exchangeName, routingKey);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//durable 是否持久化消息</span></span><br><span class="line">        QueueingConsumer consumer = <span class="keyword">new</span> QueueingConsumer(channel);</span><br><span class="line">        <span class="comment">//参数：队列名称、是否自动ACK、Consumer</span></span><br><span class="line">        channel.basicConsume(queueName, <span class="keyword">true</span>, consumer);  </span><br><span class="line">        <span class="comment">//循环获取消息  </span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;  </span><br><span class="line">            <span class="comment">//获取消息，如果没有消息，这一步将会一直阻塞  </span></span><br><span class="line">            Delivery delivery = consumer.nextDelivery();  </span><br><span class="line">            String msg = <span class="keyword">new</span> String(delivery.getBody());    </span><br><span class="line">            System.out.println(<span class="string">&quot;收到消息：&quot;</span> + msg);  </span><br><span class="line">        &#125; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/RabbitMQ%E6%9E%81%E9%80%9F%E5%85%A5%E9%97%A8-14/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/RabbitMQ%E6%9E%81%E9%80%9F%E5%85%A5%E9%97%A8-14/" class="post-title-link" itemprop="url">RabbitMQ极速入门(14)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 21:46:34" itemprop="dateCreated datePublished" datetime="2021-01-25T21:46:34+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_1.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_1.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_2.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_14_2.png"></p>
<h2 id="生产者"><a href="#生产者" class="headerlink" title="生产者"></a>生产者</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Sender</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//	1 创建ConnectionFactory</span></span><br><span class="line">		ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory();</span><br><span class="line">		connectionFactory.setHost(<span class="string">&quot;192.168.11.71&quot;</span>);</span><br><span class="line">		connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//	2 创建Connection</span></span><br><span class="line">		Connection connection = connectionFactory.newConnection();</span><br><span class="line">		<span class="comment">//	3 创建Channel</span></span><br><span class="line">		Channel channel = connection.createChannel();  </span><br><span class="line">		<span class="comment">//	4 声明</span></span><br><span class="line">		String queueName = <span class="string">&quot;test001&quot;</span>;  </span><br><span class="line">        <span class="comment">//	参数: queue名字,是否持久化,独占的queue（仅供此连接）,不使用时是否自动删除, 其他参数</span></span><br><span class="line">		channel.queueDeclare(queueName, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);</span><br><span class="line">		</span><br><span class="line">		Map&lt;String, Object&gt; headers = <span class="keyword">new</span> HashMap&lt;String, Object&gt;();</span><br><span class="line">		</span><br><span class="line">		AMQP.BasicProperties props = <span class="keyword">new</span> AMQP.BasicProperties.Builder()</span><br><span class="line">		.deliveryMode(<span class="number">2</span>)</span><br><span class="line">		.contentEncoding(<span class="string">&quot;UTF-8&quot;</span>)</span><br><span class="line">		.headers(headers).build();</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>;i++) &#123;</span><br><span class="line">			String msg = <span class="string">&quot;Hello World RabbitMQ &quot;</span> + i;</span><br><span class="line">			channel.basicPublish(<span class="string">&quot;&quot;</span>, queueName , props , msg.getBytes()); 			</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="消费者"><a href="#消费者" class="headerlink" title="消费者"></a>消费者</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Receiver</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		</span><br><span class="line">        ConnectionFactory connectionFactory = <span class="keyword">new</span> ConnectionFactory() ;  </span><br><span class="line">        </span><br><span class="line">        connectionFactory.setHost(<span class="string">&quot;192.168.11.71&quot;</span>);</span><br><span class="line">        connectionFactory.setPort(<span class="number">5672</span>);</span><br><span class="line">		connectionFactory.setVirtualHost(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">		</span><br><span class="line">        connectionFactory.setAutomaticRecoveryEnabled(<span class="keyword">true</span>);</span><br><span class="line">        connectionFactory.setNetworkRecoveryInterval(<span class="number">3000</span>);</span><br><span class="line">        Connection connection = connectionFactory.newConnection();</span><br><span class="line">        </span><br><span class="line">        Channel channel = connection.createChannel();  </span><br><span class="line">        </span><br><span class="line">        String queueName = <span class="string">&quot;test001&quot;</span>;  </span><br><span class="line">        <span class="comment">//	durable 是否持久化消息</span></span><br><span class="line">        channel.queueDeclare(queueName, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">null</span>);  </span><br><span class="line">        QueueingConsumer consumer = <span class="keyword">new</span> QueueingConsumer(channel);</span><br><span class="line">        <span class="comment">//	参数：队列名称、是否自动ACK、Consumer</span></span><br><span class="line">        channel.basicConsume(queueName, <span class="keyword">true</span>, consumer);  </span><br><span class="line">        <span class="comment">//	循环获取消息  </span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;  </span><br><span class="line">            <span class="comment">//	获取消息，如果没有消息，这一步将会一直阻塞  </span></span><br><span class="line">            Delivery delivery = consumer.nextDelivery();  </span><br><span class="line">            String msg = <span class="keyword">new</span> String(delivery.getBody());    </span><br><span class="line">            System.out.println(<span class="string">&quot;收到消息：&quot;</span> + msg);  </span><br><span class="line">        &#125; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/RabbitMQ%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E4%B8%8E%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%AF%A6%E8%A7%A3-13/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/RabbitMQ%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E4%B8%8E%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%AF%A6%E8%A7%A3-13/" class="post-title-link" itemprop="url">RabbitMQ环境搭建与控制台详解(13)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 21:45:28" itemprop="dateCreated datePublished" datetime="2021-01-25T21:45:28+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_13_1.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_13_1.png"></p>
<h1 id="RabbitMQ急速入门"><a href="#RabbitMQ急速入门" class="headerlink" title="RabbitMQ急速入门"></a>RabbitMQ急速入门</h1><p>急速入门，在这里我们使用RabbitMQ 3.6.5 版本进行操作：</p>
<ul>
<li>环境搭建：</li>
<li>官网地址：<a target="_blank" rel="noopener" href="http://www.rabbitmq.com/">http://www.rabbitmq.com/</a></li>
<li>环境描述：Linux（centos7 Redhat7）</li>
</ul>
<h2 id="1-首先在Linux上进行一些软件的准备工作，yum下来一些基础的软件包"><a href="#1-首先在Linux上进行一些软件的准备工作，yum下来一些基础的软件包" class="headerlink" title="1. 首先在Linux上进行一些软件的准备工作，yum下来一些基础的软件包"></a>1. 首先在Linux上进行一些软件的准备工作，yum下来一些基础的软件包</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz</span><br></pre></td></tr></table></figure>

<ul>
<li><ul>
<li>配置好主机名称**：<code>/etc/hosts /etc/hostname</code></li>
</ul>
</li>
</ul>
<h2 id="2-下载RabbitMQ所需软件包（我在在这里使用的是-RabbitMQ3-6-5-稳定版本）"><a href="#2-下载RabbitMQ所需软件包（我在在这里使用的是-RabbitMQ3-6-5-稳定版本）" class="headerlink" title="2. 下载RabbitMQ所需软件包（我在在这里使用的是 RabbitMQ3.6.5 稳定版本）"></a>2. 下载RabbitMQ所需软件包（我在在这里使用的是 RabbitMQ3.6.5 稳定版本）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wget www.rabbitmq.com&#x2F;releases&#x2F;erlang&#x2F;erlang-18.3-1.el7.centos.x86_64.rpm</span><br><span class="line">wget &lt;http:&#x2F;&#x2F;repo.iotti.biz&#x2F;CentOS&#x2F;7&#x2F;x86_64&#x2F;socat-1.7.3.2-1.1.el7.lux.x86_64.rpm&gt;</span><br><span class="line">wget www.rabbitmq.com&#x2F;releases&#x2F;rabbitmq-server&#x2F;v3.6.5&#x2F;rabbitmq-server-3.6.5-1.noarch.rpm</span><br></pre></td></tr></table></figure>

<h2 id="3-安装服务命令"><a href="#3-安装服务命令" class="headerlink" title="3. 安装服务命令"></a>3. 安装服务命令</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">rpm -ivh erlang-18.3-1.el7.centos.x86_64.rpm </span><br><span class="line">rpm -ivh socat-1.7.3.2-1.1.el7.x86_64.rpm</span><br><span class="line">rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm</span><br></pre></td></tr></table></figure>

<h2 id="4-修改用户登录与连接心跳检测，注意修改"><a href="#4-修改用户登录与连接心跳检测，注意修改" class="headerlink" title="4. 修改用户登录与连接心跳检测，注意修改"></a>4. 修改用户登录与连接心跳检测，注意修改</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim &#x2F;usr&#x2F;lib&#x2F;rabbitmq&#x2F;lib&#x2F;rabbitmq_server-3.6.5&#x2F;ebin&#x2F;rabbit.app</span><br></pre></td></tr></table></figure>

<p>修改点1：loopback_users 中的 &lt;&lt;”guest”&gt;&gt;,只保留guest （用于用户登录） 修改点2：heartbeat 为10（用于心跳连接）</p>
<h2 id="5-安装管理插件"><a href="#5-安装管理插件" class="headerlink" title="5. 安装管理插件"></a>5. 安装管理插件</h2><h2 id="5-1-首先启动服务-后面-包含了停止、查看状态以及重启的命令"><a href="#5-1-首先启动服务-后面-包含了停止、查看状态以及重启的命令" class="headerlink" title="5.1 首先启动服务(后面 | 包含了停止、查看状态以及重启的命令)"></a>5.1 首先启动服务(后面 | 包含了停止、查看状态以及重启的命令)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;etc&#x2F;init.d&#x2F;rabbitmq-server start | stop | status | restart</span><br></pre></td></tr></table></figure>

<h2 id="5-2-查看服务有没有启动：-lsof-i-5672-（5672是Rabbit的默认端口）"><a href="#5-2-查看服务有没有启动：-lsof-i-5672-（5672是Rabbit的默认端口）" class="headerlink" title="5.2 查看服务有没有启动： lsof -i:5672 （5672是Rabbit的默认端口）"></a>5.2 查看服务有没有启动： lsof -i:5672 （5672是Rabbit的默认端口）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rabbitmq-plugins enable rabbitmq_management</span><br></pre></td></tr></table></figure>

<h2 id="5-3-可查看管理端口有没有启动："><a href="#5-3-可查看管理端口有没有启动：" class="headerlink" title="5.3 可查看管理端口有没有启动："></a>5.3 可查看管理端口有没有启动：</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsof -i:15672 或者 netstat -tnlp | grep 15672</span><br></pre></td></tr></table></figure>

<h2 id="6-一切OK-我们访问地址，输入用户名密码均为-guest-："><a href="#6-一切OK-我们访问地址，输入用户名密码均为-guest-：" class="headerlink" title="6. 一切OK 我们访问地址，输入用户名密码均为 guest ："></a>6. 一切OK 我们访问地址，输入用户名密码均为 guest ：</h2><p><a target="_blank" rel="noopener" href="http://你的ip地址:15672/">http://你的ip地址:15672/</a></p>
<h2 id="7-如果一切顺利，那么到此为止，我们的环境已经安装完啦"><a href="#7-如果一切顺利，那么到此为止，我们的环境已经安装完啦" class="headerlink" title="7. 如果一切顺利，那么到此为止，我们的环境已经安装完啦"></a>7. 如果一切顺利，那么到此为止，我们的环境已经安装完啦</h2>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/RabbitMQ%E5%AE%9E%E6%88%98-%E5%88%9D%E8%AF%86RabbitMQ%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5-12/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/RabbitMQ%E5%AE%9E%E6%88%98-%E5%88%9D%E8%AF%86RabbitMQ%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5-12/" class="post-title-link" itemprop="url">RabbitMQ实战-初识RabbitMQ核心概念(12)</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 21:44:01" itemprop="dateCreated datePublished" datetime="2021-01-25T21:44:01+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mq/" itemprop="url" rel="index"><span itemprop="name">mq</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-前言"><a href="#1-前言" class="headerlink" title="1 前言"></a>1 前言</h1><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/sgh1023/p/11217017.html">RabbitMQ（一）：RabbitMQ快速入门</a></p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/williamjie/p/9481774.html">RabbitMQ基础概念详细介绍</a></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_1.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_1.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_2.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_2.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_3.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_3.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_4.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_4.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_5.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_5.png"></p>
<h1 id="2-AMQP协议中间的几个重要概念："><a href="#2-AMQP协议中间的几个重要概念：" class="headerlink" title="2 AMQP协议中间的几个重要概念："></a>2 AMQP协议中间的几个重要概念：</h1><p>下图是AMQP的协议模型：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_6.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_6.png"></p>
<p>正如图中所看到的，AMQP协议模型有三部分组成：生产者、消费者和服务端。</p>
<p>生产者是投递消息的一方，首先连接到Server，建立一个连接，开启一个信道；然后生产者声明交换器和队列，设置相关属性，并通过路由键将交换器和队列进行绑定。同理，消费者也需要进行建立连接，开启信道等操作，便于接收消息。</p>
<p>接着生产者就可以发送消息，发送到服务端中的虚拟主机，虚拟主机中的交换器根据路由键选择路由规则，然后发送到不同的消息队列中，这样订阅了消息队列的消费者就可以获取到消息，进行消费。</p>
<p>最后还要关闭信道和连接。</p>
<ul>
<li>Server：又称Broker。接收客户端的连接，实现AMQP实体服务。</li>
<li>Connection：连接，应用程序与Server的网络连接，TCP连接。</li>
<li>Channel：信道，消息读写（几乎所有操作）等操作在信道中进行。客户端可以建立多个信道，每个信道代表一个会话任务。</li>
<li>Message：消息，应用程序和服务器之间传送的数据，消息可以非常简单，也可以很复杂。有Properties和Body组成。Properties为外包装，可以对消息进行修饰，比如消息的优先级、延迟等高级特性；Body就是消息体内容。</li>
<li>Virtual Host：虚拟主机，用于逻辑隔离。一个虚拟主机里面可以有若干个Exchange和Queue，同一个虚拟主机里面不能有相同名称的Exchange或Queue。</li>
<li>Exchange：交换器，接收消息，按照路由规则将消息路由到一个或者多个队列。如果路由不到，或者返回给生产者，或者直接丢弃。RabbitMQ常用的交换器常用类型有direct、topic、fanout、headers四种，后面详细介绍。</li>
<li>Binding：绑定，交换器和消息队列之间的虚拟连接，绑定中可以包含一个或者多个RoutingKey。</li>
<li>RoutingKey：路由键，生产者将消息发送给交换器的时候，会发送一个RoutingKey，用来指定<strong>路由规则</strong>，这样交换器就知道把消息发送到哪个队列。路由键通常为一个“.”分割的字符串，例如“com.rabbitmq”。</li>
<li>Queue：消息队列，用来保存消息，供消费者消费。</li>
</ul>
<blockquote>
<p>我们完全可以直接使用 Connection 就能完成信道的工作，为什么还要引入信道呢?</p>
</blockquote>
<blockquote>
<p>试想这样一个场景， 一个应用程序中有很多个线程需要从 RabbitMQ 中消费消息，或者生产消息，那么必然需要建立很多个 Connection，也就是许多个 TCP 连接。然而对于操作系统而言，建立和销毁 TCP 连接是非常昂贵的开销，如果遇到使用高峰，性能瓶颈也随之显现。 RabbitMQ 采用 TCP 连接复用的方式，不仅可以减少性能开销，同时也便于管理 。</p>
</blockquote>
<p>RabbitMQ是基于AMQP协议实现的，其结构如下图所示，和AMQP协议简直就是一模一样。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_7.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_7.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_11.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_11.png"></p>
<h1 id="3-常用交换器"><a href="#3-常用交换器" class="headerlink" title="3 常用交换器"></a>3 <strong>常用交换器</strong></h1><p>RabbitMQ常用的交换器类型有direct、topic、fanout、headers四种。</p>
<h2 id="3-1-Direct-Exchange"><a href="#3-1-Direct-Exchange" class="headerlink" title="3.1 Direct Exchange"></a>3.1 Direct Exchange</h2><p>该类型的交换器将所有发送到该交换器的消息被转发到RoutingKey指定的队列中，也就是说路由到BindingKey和RoutingKey完全匹配的队列中。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_8.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_8.png"></p>
<h2 id="3-2-Topic-Exchange"><a href="#3-2-Topic-Exchange" class="headerlink" title="3.2 Topic Exchange"></a>3.2 Topic Exchange</h2><p>该类型的交换器将所有发送到Topic Exchange的消息被转发到所有RoutingKey中指定的Topic的队列上面。</p>
<p>Exchange将RoutingKey和某Topic进行模糊匹配，其中“<em>”用来匹配一个词，“#”用于匹配一个或者多个词。例如“com.#”能匹配到“com.rabbitmq.oa”和“com.rabbitmq”；而”login.</em>“只能匹配到“com.rabbitmq”。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_9.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_9.png"></p>
<h2 id="3-3-Fanout-Exchange"><a href="#3-3-Fanout-Exchange" class="headerlink" title="3.3 Fanout Exchange"></a>3.3 Fanout Exchange</h2><p>该类型不处理路由键，会把所有发送到交换器的消息路由到所有绑定的队列中。优点是转发消息最快，性能最好。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_10.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/rabbitmq_11_10.png"></p>
<h2 id="3-4-Headers-Exchange"><a href="#3-4-Headers-Exchange" class="headerlink" title="3.4 Headers Exchange"></a>3.4 Headers Exchange</h2><p>该类型的交换器不依赖路由规则来路由消息，而是根据消息内容中的headers属性进行匹配。headers类型交换器性能差，在实际中并不常用。</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/25/Java%E5%A4%9A%E7%BA%BF%E7%A8%8BJUC%E5%8E%9F%E5%AD%90%E7%B1%BB%E4%B9%8B-AtomicLong%E5%8E%9F%E5%AD%90%E7%B1%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/01/25/Java%E5%A4%9A%E7%BA%BF%E7%A8%8BJUC%E5%8E%9F%E5%AD%90%E7%B1%BB%E4%B9%8B-AtomicLong%E5%8E%9F%E5%AD%90%E7%B1%BB/" class="post-title-link" itemprop="url">Java多线程JUC原子类之 AtomicLong原子类</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-25 17:10:03" itemprop="dateCreated datePublished" datetime="2021-01-25T17:10:03+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:41:58" itemprop="dateModified" datetime="2022-05-10T09:41:58+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <blockquote>
<p>转载自<a target="_blank" rel="noopener" href="https://www.cnblogs.com/skywang12345/p/3514593.html">https://www.cnblogs.com/skywang12345/p/3514593.html</a></p>
</blockquote>
<h1 id="1-简介"><a href="#1-简介" class="headerlink" title="1 简介"></a>1 简介</h1><p>AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似。所以以AtomicLong对基本类型的原子类进行介绍。内容主要包括：</p>
<ul>
<li>AtomicLong介绍和函数列表</li>
<li>AtomicLong源码分析(基于JDK1.8.0_251)</li>
<li>AtomicLong示例</li>
</ul>
<h1 id="2-AtomicLong介绍和函数列表"><a href="#2-AtomicLong介绍和函数列表" class="headerlink" title="2 AtomicLong介绍和函数列表"></a>2 AtomicLong介绍和函数列表</h1><p>AtomicLong是作用是对长整形进行原子操作。</p>
<p>在32位操作系统中，64位的long 和 double 变量由于会被JVM当作两个分离的32位来进行操作，所以不具有原子性。而使用AtomicLong能让long的操作保持原子型。</p>
<h2 id="2-1-函数列表"><a href="#2-1-函数列表" class="headerlink" title="2.1 函数列表"></a>2.1 函数列表</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 构造函数</span></span><br><span class="line">AtomicLong()</span><br><span class="line"><span class="comment">// 创建值为initialValue的AtomicLong对象</span></span><br><span class="line">AtomicLong(<span class="keyword">long</span> initialValue)</span><br><span class="line"><span class="comment">// 以原子方式设置当前值为newValue。</span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">long</span> newValue)</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 获取当前值</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">get</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 以原子方式将当前值减 1，并返回减1后的值。等价于“--num”</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">decrementAndGet</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 以原子方式将当前值减 1，并返回减1前的值。等价于“num--”</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">getAndDecrement</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 以原子方式将当前值加 1，并返回加1后的值。等价于“++num”</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">incrementAndGet</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 以原子方式将当前值加 1，并返回加1前的值。等价于“num++”</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">getAndIncrement</span><span class="params">()</span>    </span></span><br><span class="line"><span class="function"><span class="comment">// 以原子方式将delta与当前值相加，并返回相加后的值。</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">addAndGet</span><span class="params">(<span class="keyword">long</span> delta)</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 以原子方式将delta添加到当前值，并返回相加前的值。</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">getAndAdd</span><span class="params">(<span class="keyword">long</span> delta)</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 如果当前值 == expect，则以原子方式将该值设置为update。成功返回true，否则返回false，并且不修改原值。</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">boolean</span> <span class="title">compareAndSet</span><span class="params">(<span class="keyword">long</span> expect, <span class="keyword">long</span> update)</span></span></span><br><span class="line"><span class="function"><span class="comment">// 以原子方式设置当前值为newValue，并返回旧值。</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">long</span> <span class="title">getAndSet</span><span class="params">(<span class="keyword">long</span> newValue)</span></span></span><br><span class="line"><span class="function"><span class="comment">// 返回当前值对应的int值</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">intValue</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"><span class="comment">// 获取当前值对应的long值</span></span></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="title">longValue</span><span class="params">()</span>    </span></span><br><span class="line"><span class="function"><span class="comment">// 以 float 形式返回当前值</span></span></span><br><span class="line"><span class="function"><span class="keyword">float</span> <span class="title">floatValue</span><span class="params">()</span>    </span></span><br><span class="line"><span class="function"><span class="comment">// 以 double 形式返回当前值</span></span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">doubleValue</span><span class="params">()</span>    </span></span><br><span class="line"><span class="function"><span class="comment">// 最后设置为给定值。延时设置变量值，这个等价于set()方法，但是由于字段是volatile类型的，因此次字段的修改会比普通字段（非volatile字段）有稍微的性能延时（尽管可以忽略），所以如果不是想立即读取设置的新值，允许在“后台”修改值，那么此方法就很有用。如果还是难以理解，这里就类似于启动一个后台线程如执行修改新值的任务，原线程就不等待修改结果立即返回（这种解释其实是不正确的，但是可以这么理解）。</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">void</span> <span class="title">lazySet</span><span class="params">(<span class="keyword">long</span> newValue)</span></span></span><br><span class="line"><span class="function"><span class="comment">// 如果当前值 == 预期值，则以原子方式将该设置为给定的更新值。JSR规范中说：以原子方式读取和有条件地写入变量但不 创建任何 happen-before 排序，因此不提供与除 weakCompareAndSet 目标外任何变量以前或后续读取或写入操作有关的任何保证。大意就是说调用weakCompareAndSet时并不能保证不存在happen-before的发生（也就是可能存在指令重排序导致此操作失败）。但是从Java源码来看，其实此方法并没有实现JSR规范的要求，最后效果和compareAndSet是等效的，都调用了unsafe.compareAndSwapInt()完成操作。</span></span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">boolean</span> <span class="title">weakCompareAndSet</span><span class="params">(<span class="keyword">long</span> expect, <span class="keyword">long</span> update)</span></span></span><br></pre></td></tr></table></figure>

<h1 id="3-AtomicLong-源码分析"><a href="#3-AtomicLong-源码分析" class="headerlink" title="3 AtomicLong 源码分析"></a>3 AtomicLong 源码分析</h1><ul>
<li>AtomicLong源码分析</li>
</ul>
<p>AtomicLong的代码很简单，下面仅以incrementAndGet()为例，对AtomicLong的原理进行说明。</p>
<p>incrementAndGet()源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">long</span> <span class="title">incrementAndGet</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (;;) &#123;</span><br><span class="line">        <span class="comment">// 获取AtomicLong当前对应的long值</span></span><br><span class="line">        <span class="keyword">long</span> current = get();</span><br><span class="line">        <span class="comment">// 将current加1</span></span><br><span class="line">        <span class="keyword">long</span> next = current + <span class="number">1</span>;</span><br><span class="line">        <span class="comment">// 通过CAS函数，更新current的值</span></span><br><span class="line">        <span class="keyword">if</span> (compareAndSet(current, next))</span><br><span class="line">            <span class="keyword">return</span> next;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：</p>
<p>(01) incrementAndGet()首先会根据get()获取AtomicLong对应的long值。该值是volatile类型的变量，get()的源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// value是AtomicLong对应的long值</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">long</span> value;</span><br><span class="line"><span class="comment">// 返回AtomicLong对应的long值</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">long</span> <span class="title">get</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> value;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>(02) incrementAndGet()接着将current加1,然后通过CAS函数，将新的值赋值给value。</p>
<p>compareAndSet()的源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">boolean</span> <span class="title">compareAndSet</span><span class="params">(<span class="keyword">long</span> expect, <span class="keyword">long</span> update)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> unsafe.compareAndSwapLong(<span class="keyword">this</span>, valueOffset, expect, update);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>compareAndSet()的作用是更新AtomicLong对应的long值。它会比较AtomicLong的原始值是否与expect相等，若相等的话，则设置AtomicLong的值为update。</p>
<h1 id="4-AtomicLong示例"><a href="#4-AtomicLong示例" class="headerlink" title="4 AtomicLong示例"></a>4 AtomicLong示例</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// LongTest.java的源码</span></span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.atomic.AtomicLong;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LongTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 新建AtomicLong对象</span></span><br><span class="line">        AtomicLong mAtoLong = <span class="keyword">new</span> AtomicLong();</span><br><span class="line"></span><br><span class="line">        mAtoLong.set(<span class="number">0x0123456789ABCDEFL</span>);</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;get()&quot;</span>, mAtoLong.get());</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;intValue()&quot;</span>, mAtoLong.intValue());</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;longValue()&quot;</span>, mAtoLong.longValue());</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : %s\n&quot;</span>, <span class="string">&quot;doubleValue()&quot;</span>, mAtoLong.doubleValue());</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : %s\n&quot;</span>, <span class="string">&quot;floatValue()&quot;</span>, mAtoLong.floatValue());</span><br><span class="line"></span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;getAndDecrement()&quot;</span>, mAtoLong.getAndDecrement());</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;decrementAndGet()&quot;</span>, mAtoLong.decrementAndGet());</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;getAndIncrement()&quot;</span>, mAtoLong.getAndIncrement());</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;incrementAndGet()&quot;</span>, mAtoLong.incrementAndGet());</span><br><span class="line"></span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;addAndGet(0x10)&quot;</span>, mAtoLong.addAndGet(<span class="number">0x10</span>));</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;getAndAdd(0x10)&quot;</span>, mAtoLong.getAndAdd(<span class="number">0x10</span>));</span><br><span class="line"></span><br><span class="line">        System.out.printf(<span class="string">&quot;\n%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;get()&quot;</span>, mAtoLong.get());</span><br><span class="line"></span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : %s\n&quot;</span>, <span class="string">&quot;compareAndSet()&quot;</span>, mAtoLong.compareAndSet(<span class="number">0x12345679L</span>, <span class="number">0xFEDCBA9876543210L</span>));</span><br><span class="line">        System.out.printf(<span class="string">&quot;%20s : 0x%016X\n&quot;</span>, <span class="string">&quot;get()&quot;</span>, mAtoLong.get());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">get() : <span class="number">0x0123456789ABCDEF</span></span><br><span class="line">          intValue() : <span class="number">0x0000000089ABCDEF</span></span><br><span class="line">         longValue() : <span class="number">0x0123456789ABCDEF</span></span><br><span class="line">       doubleValue() : <span class="number">8.1985529216486896E16</span></span><br><span class="line">        floatValue() : <span class="number">8.1985531E16</span></span><br><span class="line">   getAndDecrement() : <span class="number">0x0123456789ABCDEF</span></span><br><span class="line">   decrementAndGet() : <span class="number">0x0123456789ABCDED</span></span><br><span class="line">   getAndIncrement() : <span class="number">0x0123456789ABCDED</span></span><br><span class="line">   incrementAndGet() : <span class="number">0x0123456789ABCDEF</span></span><br><span class="line">     addAndGet(<span class="number">0x10</span>) : <span class="number">0x0123456789ABCDFF</span></span><br><span class="line">     getAndAdd(<span class="number">0x10</span>) : <span class="number">0x0123456789ABCDFF</span></span><br><span class="line"></span><br><span class="line">               get() : <span class="number">0x0123456789ABCE0F</span></span><br><span class="line">     compareAndSet() : <span class="keyword">false</span></span><br><span class="line">               get() : <span class="number">0x0123456789ABCE0F</span></span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/11/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/11/">11</a><span class="page-number current">12</span><a class="page-number" href="/page/13/">13</a><span class="space">&hellip;</span><a class="page-number" href="/page/24/">24</a><a class="extend next" rel="next" href="/page/13/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
